package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.BaseContext;
import com.example.common.CustomerExcaption;
import com.example.common.IdWorker;
import com.example.common.R;
import com.example.domain.Category;
import com.example.service.CategoryServicce;
import com.example.service.DishService;
import com.example.service.SetmealService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
@ResponseBody
public class CategoryController {
    @Autowired
    private CategoryServicce categoryServicce;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @GetMapping("/page")
    public R<Page> getCategoryPage(int page,int pageSize)
    {
        int pagecount=(page-1)*pageSize;
        Page<Category> p=new Page<>(page,pageSize);
        List<Category> categorylists = categoryServicce.getCategorylists(pagecount, pageSize);
        p.setRecords(categorylists);
        Long selectcount = categoryServicce.selectcount();
        p.setTotal(selectcount);
        log.info("查询Category的全部数据");
        return R.success(p);

    }

    @PostMapping
    public R<Category> addCategory(HttpServletRequest request,@RequestBody Category category)
    {
//        Category c = categoryServicce.selectcategorybyname(category);
//        if(c!=null)
//        {
//            return R.error("当前菜名已存在");
//        }
//        else {
        IdWorker idWorker=new IdWorker();
        category.setId(idWorker.nextId());
            Long id = (Long) request.getSession().getAttribute("employee");
            category.setCreateTime(LocalDateTime.now());
            category.setUpdateTime(LocalDateTime.now());
            category.setCreateUser(id);
            category.setUpdateUser(id);
            categoryServicce.addCategory(category);
            log.info("添加成功");
            log.info(category.toString());
            return R.success(category);
//        }
    }
    @DeleteMapping
    public R<Category> deleCategory(Long ids)
    {
        log.info("删除的id为"+ids);
        //查询当前删除的分类是否关联了菜品，如果关联报异常
        int dishcount = dishService.selectByCategoryId(ids);
        if(dishcount>0)
        {
            //抛异常
            throw  new CustomerExcaption("当前分类关联菜品，不能删除");
        }

        //查询当前删除的分类是否关联了套餐，如果关联报异常
        int setmealcount = setmealService.selectByCategoryIdInt(ids);
        if(setmealcount>0)
        {
            //抛异常
            throw  new CustomerExcaption("当前套餐关联菜品，不能删除");
        }

        //正常删除风分类
        categoryServicce.deletecategory(ids);
        return R.success(null);
    }
    @PutMapping
    public R<Category> updatebyid(HttpServletRequest request,@RequestBody Category category)
    {
        category.setUpdateTime(LocalDateTime.now());
        Long getid = (Long) request.getSession().getAttribute("employee");
        category.setUpdateUser(getid);
        log.info(category.toString());
        categoryServicce.updatebyid(category);
        return R.success(category);
    }

    @GetMapping("/list")
    public R<List<Category>> getCategoryList(Integer type)
    {
        if(type==null)
        {
            List<Category> selectall = categoryServicce.selectall();
            log.info(selectall.toString());
            return R.success(selectall);
        }
        else
        {
            List<Category> categoryList = categoryServicce.getCategoryList(type);
            log.info(categoryList.toString());
            return R.success(categoryList);
        }
    }

}
